import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { DefaultStage } from "../default-stage";
import { Landscape } from "./landscape";
import { Soldier } from "./soldier";
import { Random } from "babylon-lib/tool/random";
import { AnimationFrame, Utils } from "babylon-lib/index";
import * as Data from '../../data/data';


enum SOLDIER_STATE {
    NONE,
    INVALID,
    SURVIVE,
}


class SoldierManager {

    private static _overReq = {};

    public static readonly count = 10;
    public static soldiers: Array<Soldier> = [];
    public static signedSoldiers: Array<Soldier> = [];
    public static invalidSoldiers: Array<Soldier> = [];
    public static surviveSoldiers: Array<Soldier> = [];

    public static initialize(stage: DefaultStage): void {
        this.initializeSoldiers(stage);
    }

    public static bindData(stage: DefaultStage): void {
        const _this = SoldierManager;
    }

    public static updateSoldiers(): void {
        for (let i = 0; i < this.signedSoldiers.length; i++) {
            const sd = this.signedSoldiers[i];
            switch (sd.state) {
                case SOLDIER_STATE.INVALID:
                    Data.invalidEnemyNumber.value++;
                    this.invalidSoldiers.push(sd);
                    this.signedSoldiers.splice(i, 1);
                    i--;
                    break;
                case SOLDIER_STATE.SURVIVE:
                    Data.surviveEnemyNumber.value++;
                    this.surviveSoldiers.push(sd);
                    this.signedSoldiers.splice(i, 1);
                    i--
                    break;
                case SOLDIER_STATE.NONE:
                    break;
            }
        }
        if (this.signedSoldiers.length === 0) {
            if (this.surviveSoldiers.length === 0) {
                Data.taskStatus.value = Data.taskState.success;
            } else {
                Data.taskStatus.value = Data.taskState.failed;
            }
            AnimationFrame.throttleByTime(this._overReq, 2000, () => {
                Data.gameOn.value = false;
            }, false);
        }
    }

    private static initializeSoldiers(stage: DefaultStage): void {
        const center = Landscape.coreCenter;
        const range = Landscape.coreExtend;
        const march = new Vector3().copyFrom(Landscape.zoneCenter).subtractInPlace(Landscape.coreCenter).normalize();
        march.y = 0;
        march.x += 0.01;
        march.normalize();
        for (let i = 0; i < this.count; i++) {
            const position = new Vector3(Random.float(-1, 1), 0, Random.float(-1, 1)).normalize().scaleInPlace(Random.float(0, range));
            position.addInPlace(center);
            const soldier = new Soldier('soldier+' + Utils.id, stage.mainScene, {});
            soldier.entity.position.copyFrom(position);
            soldier.turn(march);
            this.soldiers.push(soldier);
        }
        this.signedSoldiers = [...this.soldiers];
    }

}


export { SoldierManager, SOLDIER_STATE };